package com.lc.hongtai.fragment;


import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.adapter.TheHolderAdapter;
import com.lc.hongtai.view.SimpleDividerItemDecoration;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.bound.BoundViewHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicDefaultHeader;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import in.srain.cube.views.ptr.PtrUIHandler;
import in.srain.cube.views.ptr.indicator.PtrIndicator;

/**
 * Created by Administrator on 2016/4/17.
 */
public class AlreadyFragment extends AppV4Fragment {

    private PtrFrameLayout store_house_ptr_frame;
    private PtrClassicDefaultHeader header;
    private RecyclerView rv01;
    private RecyclerView.LayoutManager mLayoutManager;
    TheHolderAdapter mAdapter;

    private List<String> mDatas;
    private boolean canLoadMore = true;
    public int totalPages = 1;
    public int pageSize = 0;
    public int totalRows = 0;
    public int nowPage = 1;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_already, null)));
        ButterKnife.bind(this, view);
        initView(view);
        getData();
        mAdapter = new TheHolderAdapter(getActivity(), mDatas);
        rv01.setAdapter(mAdapter);
        initRefreshListener();
        return view;


    }




    public void getData(){
        mDatas=new ArrayList<>();
        for (int i=0;i<10;i++){
            mDatas.add("hehe"+i);
        }

    }

    public void initView(View view) {


        this.store_house_ptr_frame = (PtrFrameLayout) view.findViewById(R.id.store_house_ptr_frame);
        this.rv01 = (RecyclerView) view.findViewById(R.id.rv_01);
        rv01.setHasFixedSize(true);

        header = new PtrClassicDefaultHeader(getActivity());

        store_house_ptr_frame.setHeaderView(header);
        mLayoutManager = new LinearLayoutManager(getActivity());
        rv01.setLayoutManager(mLayoutManager);

        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider3);
        rv01.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 2));

    }


    private void initRefreshListener() {
        rv01.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findLastVisibleItemPosition();
                if (dy > 0 && canLoadMore && lastVisibleItemPosition + 1 == mAdapter.getItemCount()) {
                    canLoadMore = false;
                    if (nowPage < totalPages) {
                        //getAccessToken(true, nowPage + 1);
                    } else {
                        //UtilToast.show(InvestManageDetailActivity.this, "当前页已是最后一页");
                    }
                }
            }
        });

        store_house_ptr_frame.setPtrHandler(new PtrHandler() {


            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                int firstVisibleItemPosition = ((LinearLayoutManager) mLayoutManager).findFirstCompletelyVisibleItemPosition();
                return firstVisibleItemPosition == 0 || mDatas.size() == 0;
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //   list.clear();
              /*  nowPage = 1;
                getAccessToken(false, nowPage);*/
            }
        });
        store_house_ptr_frame.addPtrUIHandler(new PtrUIHandler() {
            @Override
            public void onUIReset(PtrFrameLayout frame) {
                header.onUIReset(frame);
            }

            @Override
            public void onUIRefreshPrepare(PtrFrameLayout frame) {
                header.onUIRefreshPrepare(frame);
            }

            @Override
            public void onUIRefreshBegin(PtrFrameLayout frame) {
                header.onUIRefreshBegin(frame);
            }

            @Override
            public void onUIRefreshComplete(PtrFrameLayout frame) {
                header.onUIRefreshComplete(frame);
            }

            @Override
            public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, PtrIndicator ptrIndicator) {
                header.onUIPositionChange(frame, isUnderTouch, status, ptrIndicator);
            }
        });
    }
}
